//
//  CreditViewController.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-5.
//
//

#import "CreditViewController.h"
#import "ConfirmCreditViewController.h"
#import <QuartzCore/QuartzCore.h>


@interface CreditViewController ()

@end

@implementation CreditViewController
{
    UITextField *_creditnumTF;
    
    UITextField *_bankTF;
    UIPickerView *_bankPicker;
    NSArray *_bankArray;
    
    UIToolbar *_toolBar;
    UIBarButtonItem *_right;
    
    UITextField *_nameTF;
    UITextField *_cashTF;
    
    UIButton *_zfbButton;
    UIButton *_ybButton;
    NSInteger _payType;
    
    UIAlertView *_baseAlert;
}

- (void)dealloc
{
    [_creditnumTF release];
    [_bankTF release];
    [_bankPicker release];
    [_bankArray release];
    [_toolBar release];
    [_right release];
    [_nameTF release];
    [_cashTF release];
    [_zfbButton release];
    [_ybButton release];
    
    _creditnumTF = nil;
    _bankTF = nil;
    _bankPicker = nil;
    _bankArray = nil;
    _toolBar = nil;
    _right = nil;
    _nameTF = nil;
    _cashTF = nil;
    _zfbButton = nil;
    _ybButton = nil;
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化视图
- (void)init_view
{
    //定义title
    self.title = @"信用卡";
    //加载背影
    [self.view setBackgroundColor:BackgroundColor];
    
    int width = [DeviceUtil getScreenWidth];
    int heigth = self.view.frame.size.height;
    
    UIScrollView *myscrollview = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, heigth)]autorelease];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = BackgroundColor;
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(width, 500);
    [myscrollview setContentSize:newSize];
    
    UIControl *control = [[[UIControl alloc] initWithFrame:myscrollview.frame] autorelease];
    [myscrollview addSubview:control];
    [control addTarget:self action:@selector(onBackgroudTouch) forControlEvents:UIControlEventTouchDown];

    //填写信用卡信息
    UIImageView *infoimgview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4.png"]];
    infoimgview.frame = CGRectMake(20, 20, 20, 20);
    [myscrollview addSubview:infoimgview];
    
    //填写信用卡信息
    UILabel *infolb = [[UILabel alloc]initWithFrame:CGRectMake(50, 20, 280, 20)];
    infolb.text = @"填写信用卡信息";
    infolb.font = [UIFont systemFontOfSize:15];
    infolb.backgroundColor = [UIColor clearColor];
    [myscrollview addSubview:infolb];
    
    //初始化信用卡号
    _creditnumTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 280, 50)];
    _creditnumTF.text = @"";
    _creditnumTF.placeholder = @"信用卡号";
    _creditnumTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _creditnumTF.textColor = [UIColor blackColor];
    _creditnumTF.keyboardType = UIKeyboardTypeNumberPad;
    _creditnumTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _creditnumTF.borderStyle = UITextBorderStyleRoundedRect;
    _creditnumTF.delegate = self ;
    _creditnumTF.returnKeyType =UIReturnKeyDone;
    _creditnumTF.layer.borderColor = [UIColor grayColor].CGColor;
    _creditnumTF.layer.borderWidth = 1 ;
    _creditnumTF.layer.cornerRadius = 10 ;
    [myscrollview addSubview:_creditnumTF];
    
    //初始化银行
    [self initbank:myscrollview];
    
    //持卡人姓名 
    _nameTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 170, 280, 50)];
    _nameTF.text = @"";
    _nameTF.placeholder = @"持卡人姓名";
    _nameTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _nameTF.textColor = [UIColor blackColor];
    _nameTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _nameTF.borderStyle = UITextBorderStyleRoundedRect;
    _nameTF.delegate = self ;
    _nameTF.returnKeyType =UIReturnKeyDone;
    _nameTF.layer.borderColor = [UIColor grayColor].CGColor;
    _nameTF.layer.borderWidth = 1 ;
    _nameTF.layer.cornerRadius = 10 ;
    [myscrollview addSubview:_nameTF];
    
    //还款金额
    _cashTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 230, 280, 50)];
    _cashTF.text = @"";
    _cashTF.placeholder = @"还款金额";
    _cashTF.keyboardType = UIKeyboardTypeNumberPad;
    _cashTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _cashTF.textColor = [UIColor blackColor];
    _cashTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _cashTF.borderStyle = UITextBorderStyleRoundedRect;
    _cashTF.delegate = self ;
    _cashTF.returnKeyType =UIReturnKeyDone;
    _cashTF.layer.borderColor = [UIColor grayColor].CGColor;
    _cashTF.layer.borderWidth = 1 ;
    _cashTF.layer.cornerRadius = 10 ;
    [myscrollview addSubview:_cashTF];
    
    //选择支付方式
    [self initPay:myscrollview];
    
    [self.view addSubview:myscrollview];
}

//选择银行
- (void)initbank:(UIScrollView*)myscrollview
{
    _bankTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 110, 280, 50)];
    _bankTF.text = @"";
    _bankTF.placeholder = @"选择银行";
    _bankTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _bankTF.textColor = [UIColor blackColor];
    _bankTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _bankTF.borderStyle = UITextBorderStyleRoundedRect;
    _bankTF.delegate = self ;
    _bankTF.returnKeyType =UIReturnKeyDone;
    _bankTF.layer.borderColor = [UIColor grayColor].CGColor;
    _bankTF.layer.borderWidth = 1 ;
    _bankTF.layer.cornerRadius = 10 ;
    [myscrollview addSubview:_bankTF];
    
    // 建立 UIToolbar
    _toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    _right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelstartPicker)];
    
    // 把按鈕加進 UIToolbar
    _toolBar.items = [NSArray arrayWithObject:_right];
    
    _bankArray = [[NSArray arrayWithObjects:@"工商银行", @"农业银行", @"中国银行", @"建设银行", @"交通银行", @"邮政储蓄银行", @"中信银行", @"光大银行", @"华夏银行", @"民生银行", @"广发银行", @"深圳发展银行", @"招商银行", @"兴业银行", @"浦发银行", @"平安银行", @"恒丰银行", @"渤海银行", @"浙商银行", @"北京银行", @"上海银行", nil] retain];
    _bankPicker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    _bankPicker.showsSelectionIndicator = YES;
    
    _bankTF.inputAccessoryView = _toolBar;
    _bankTF.delegate = self;
    _bankPicker.delegate = self;
    _bankPicker.dataSource = self;
    _bankPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    _bankTF.inputView = _bankPicker;

}

//选择支付方式
- (void)initPay:(UIScrollView*)myscrollview
{
    UILabel *label_pay = [[[UILabel alloc]initWithFrame:CGRectMake(20, 290, 180, 30)]autorelease];
    label_pay.backgroundColor = [UIColor clearColor];
    label_pay.text = @"选择支付方式:";
    [myscrollview addSubview:label_pay];
    
    _zfbButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 330, 100, 30)];
    [_zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
    [_zfbButton setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
    [_zfbButton addTarget:self action:@selector(zfbButtonPick:) forControlEvents:UIControlEventTouchDown];
    [myscrollview addSubview:_zfbButton];
    
    _ybButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 330, 100, 30)];
    [_ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
    [_ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian2"] forState:UIControlStateSelected];
    [_ybButton addTarget:self action:@selector(ybButtonPick:) forControlEvents:UIControlEventTouchDown];
    [myscrollview addSubview:_ybButton];
    
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 370, 230, 40);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"xyb"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    nextStep.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nextStep.layer.borderColor = [UIColor grayColor].CGColor;
    nextStep.layer.borderWidth = 1 ;
    nextStep.layer.cornerRadius = 10 ;
    [myscrollview addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge) forControlEvents:UIControlEventTouchUpInside];
}

-(void)zfbButtonPick:(id)sender
{
    [_zfbButton setSelected:YES];
    [_ybButton setSelected:NO];
    _payType = 0;
}

-(void)ybButtonPick:(id)sender
{
    [_zfbButton setSelected:NO];
    [_ybButton setSelected:YES];
    _payType = 1;
}

//下一步
-(void)confirmMessge
{
    if ([_creditnumTF.text isEqualToString:@""] || [_bankTF.text isEqualToString:@""] ||
        [_nameTF.text isEqualToString:@""] || [_cashTF.text isEqualToString:@""]) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"数据不完整" message:@"请填写完整的数据"  delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
        
    } else {
        ConfirmCreditViewController *confirmController = [[ConfirmCreditViewController alloc] init];
        confirmController.cardno = _creditnumTF.text;
        confirmController.bankname = _bankTF.text;
        confirmController.name = _nameTF.text;
        confirmController.cash = _cashTF.text;
        confirmController.paytype = _payType;
        [self.navigationController pushViewController:confirmController animated:YES];
        [confirmController release];
    }
    
}

- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}

#pragma picker delegate
-(void) cancelstartPicker {
    [_bankTF endEditing:YES];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [_bankArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [_bankArray objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[[UILabel alloc] init] autorelease];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSInteger row = [_bankPicker selectedRowInComponent:0];
    _bankTF.text = [_bankArray objectAtIndex:row];
}

//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}


#pragma mark UITextFieldDelegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_creditnumTF resignFirstResponder];
    [_nameTF resignFirstResponder];
    [_cashTF resignFirstResponder];
}


- (void)onBackgroudTouch
{
    [_creditnumTF resignFirstResponder];
    [_nameTF resignFirstResponder];
    [_cashTF resignFirstResponder];
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
}

@end



